package com.project.api.sale.controller;

import com.aliyuncs.exceptions.ClientException;
import com.project.api.common.Http;
import com.project.api.common.SmsDemo;
import com.project.api.sale.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.io.IOException;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;

@RestController
public class HTController {
    @Autowired
    public AdminService adminService;

    /**
     * 跳转后台登录页
     * @return
     */
    @RequestMapping("/htlogin")
    public ModelAndView HTLogin(){
        return new ModelAndView("htlogin.html");
    }

    /**
     * 跳转后台首页
     * @return
     */
    @RequestMapping("/htindex")
    public ModelAndView HTIndex(){
        return new ModelAndView("htindex.html");
    }

    /**
     * 后台首页内跳转欢迎页
     * @return
     */
    @RequestMapping("/welcome")
    public ModelAndView welcomeIndex(){
        ModelAndView mv = new ModelAndView("welcome.html");
        return mv;
    }

    /**
     * 跳转用户列表页面
     * @return
     */
    @RequestMapping("/userlist")
    public ModelAndView userListIndex(){
        ModelAndView mv = new ModelAndView("userlist.html");
        List list = adminService.queryUserData();
        mv.addObject("userdata", list);

        return mv;
    }

    /**
     * 跳转订单列表界面
     * @return
     */
    @RequestMapping("/orderlist")
    public ModelAndView orderListIndex(@RequestParam(name = "filter") String filter){
        ModelAndView mv = new ModelAndView("orderlist.html");
//        Map map = adminService.queryOrderList("all");
//        mv.addObject("orderdata", map);
        mv.addObject("filter", filter);
        return mv;
    }

    @RequestMapping("/orderXg")
    public ModelAndView orderXGIndex(@RequestParam(name = "id") String id){
        ModelAndView mv = new ModelAndView("order-xg.html");
        mv.addObject("id", id);
        return mv;
    }

    /**
     * 获取订单列表
     * @return
     */
    @RequestMapping("/getorderlist")
    public Map getOrderList(@RequestParam(name = "filter") String filter){
        Map map = adminService.queryOrderList(filter);
        return map;
    }

    /**
     * 跳转到用户修改页面
     * @param openid
     * @return
     */
    @RequestMapping("/member-add")
    public ModelAndView memberAddtIndex(@RequestParam(name = "openid") String openid){
        ModelAndView mv = new ModelAndView("member-add.html");
        Map map = adminService.queryPersonData(openid);
        mv.addObject("person", map);
        return mv;
    }

    /**
     * 修改用户信息
     * @param openid
     * @param username
     * @param phone
     * @param address
     */
    @RequestMapping("/updateperson")
    public void updatePerson(@RequestParam(name = "openid") String openid,
                             @RequestParam(name = "username") String username,
                             @RequestParam(name = "phone") String phone,
                             @RequestParam(name = "address") String address){
        adminService.updatePersonData(openid, username, phone, address);

    }

    /**
     * 首页获取总量信息
     * @return
     */
    @RequestMapping("/gettotaldata")
    public Map getTotalData(){
        List list = adminService.queryTotalData();
        Map map = (Map) list.get(0);
        return map;
    }

    /**
     * 获取用户信息
     * @return
     */
    @RequestMapping("/getuserdata")
    public List getUserlData(){
        List list = adminService.queryUserData();
        return list;
    }

    /**
     * 删除用户数据
     * @param openid
     */
    @RequestMapping("/deletePerson")
    public void deletePerson(@RequestParam(name = "openid") String openid){
        adminService.deletePersonData(openid);
    }

    /**
     * 更改订单退款信息
     * @param id
     */
    @RequestMapping("/refundOrder")
    public void deleteOrder(@RequestParam(name = "id") String id) {
//        String firstdata = "系统接收到信息订单，请尽快前往处理！";
//        double totalSum  = 520.1155;
//        String productName = "宝马一台";
//        String remark = "这是小景的测试消息";
//
//        Http http = new Http();
//        List list = adminService.getCustomerService();
//        if (list != null){
//            for (int i=0; i<list.size(); i++){
//                Map tmp = (Map)list.get(i);
//                String openid = tmp.get("openid").toString();
//                try {
//                    http.sendOrderPayMsg(openid, firstdata, totalSum, productName, remark);
//                } catch (Exception e) {
//                    e.printStackTrace();
//                }
//            }
//        }

        adminService.refundOrder(id);
    }

    /**
     * 根据订单id查找订单信息
     * @param id
     * @return
     */
    @RequestMapping("/getOrderById")
    public List getOrderById(@RequestParam(name = "id") String id){
        List list = adminService.querOrderListById(id);
        return list;
    }

    /**
     * 检查登录是否合法
     * @param username
     * @param password
     * @return
     */
    @RequestMapping("/checkadmin")
    public String checkAdmin(@RequestParam(name = "username") String username,
                             @RequestParam(name = "password") String password){
        String pwd = adminService.queryPassword(username);
        if (pwd.equals("--null--")){
            return "none user";
        } else {
            if (pwd.equals(password)){
                return "success";
            }else {
                return "error";
            }
        }
    }

    /**
     * 更改订单的状态为已对公转账
     * @param id
     * @return
     */
    @RequestMapping("/changeStatus")
    public String payOrder(@RequestParam(name = "id") String id,
                           @RequestParam(name = "status") String status) throws IOException, ClientException {
        String changeData = "";
        String openid = "";
        String firstData = "亲，您购买的商品已经发货了，请耐心等候商品的到来哦~";
        String payTime = "";
        String productName = "";
        String totalSum = "";
        String name = "";
        String number = "";
        String phone = "";
        String remark = "若有疑问，请联系温氏乳业客服，电话：0758-3988098。";
        if (status.equals("0")){
            changeData = "2";
        } else if (status.equals("1") || status.equals("2")){
            changeData = "3";
            List list = adminService.queryOrderDataById(id);
            if (list != null && list.size() != 0){
                Map map = (Map) list.get(0);
                String date = map.get("createTime").toString();
                Long ts = Long.valueOf(date).longValue();
                Timestamp timestamp = new Timestamp(ts) ;
                DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                payTime = sdf.format(timestamp);
                totalSum = map.get("total").toString();
                openid = map.get("openid").toString();
                name = map.get("username").toString();
                number = map.get("number").toString();
                phone = map.get("phone").toString();

                for (int i=0; i<list.size(); i++){
                    Map map1 = (Map) list.get(i);
                    if (i > 0){
                        productName += "\n";
                    }
                    productName += map1.get("tcname") + " " + map1.get("attribute") +  " 【" + map1.get("qty") + "份】";
                }
            }
            Http.sendDeliveryMsg(openid, firstData, payTime, productName, totalSum, remark);
            SmsDemo.sendSms(name, phone, number);
        } else if (status.equals("3") || status.equals("4")){
            changeData = "5";
        }
        adminService.changeOrder(id, changeData);

        return "success";
    }
}
